<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>电子时钟</title>
    <style>
        .clock{
            padding: 20px 30px;
            border:solid 1px #666;
            display:inline-block;
        }
    </style>
</head>
<body>
    <button>来个电子时钟</button>
    <button>下去吧</button>
    <hr>

    <div id="root"></div>

    <script src="../js/react.development.js"></script>
    <script src="../js/react-dom.development.js"></script>
    <script src="../js/babel.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

    <script type="text/babel">
        //format 格式化
        // console.log(moment().format('YYYY-MM-DD HH:mm:ss'))
        class Clock extends React.Component{

            //声明状态
            state = {
                time: moment().format('YYYY-MM-DD HH:mm:ss')
            }

            render(){
                return <div className="clock">
                        {this.state.time}
                    </div>
            }

            //组件挂载完毕回调  挂载完成之后执行 1 次
            componentDidMount(){
                this.timer = setInterval(() => {
                    console.log(111);
                    this.setState({
                        time: moment().format('YYYY-MM-DD HH:mm:ss')
                    })
                }, 1000);
            }

            //组件将要卸载
            componentWillUnmount(){
                //停止定时器
                clearInterval(this.timer);
            }
        }



        let btns = document.querySelectorAll('button');

        //绑定事件
        btns[0].onclick = function(){
            //渲染虚拟 DOM
            ReactDOM.render(<Clock />, document.querySelector("#root"));
        }

        btns[1].onclick = function(){
            //卸载组件             at 在...  node 节点
            ReactDOM.unmountComponentAtNode(document.querySelector("#root"));
        }
    </script>
</body>
</html>